/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui2;

import gui2.test.Observable;
import bn.Predef;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;

/**
 *
 * @author Jun Ling
 * MainJFrame is the 'main' View.
 * It presents GUI elements which allow user interaction with the system. 
 */
public class MainJFrame extends javax.swing.JFrame{

    GraphPanel graphPanel;
    ArrayList<JButton> BtnArr = new ArrayList<>();
    public boolean usingButtons;
    /**
     * Creates new form MainJFrame
     * @param usebuttons
     */
    public MainJFrame(boolean usebuttons) {
        usingButtons = usebuttons;
        initComponents();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("bnKit");
        this.setResizable(false);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addNodePanel = new javax.swing.JPanel();
        drawPanel = new javax.swing.JPanel();
        panelContainerPanel = new javax.swing.JPanel();
        actionsPanel = new javax.swing.JPanel();
        deleteSelectedBtn = new javax.swing.JButton();
        deleteAllBtn = new javax.swing.JButton();
        applyLayoutBtn = new javax.swing.JButton();
        jButtonRefresh = new javax.swing.JButton();
        queryPanel = new javax.swing.JPanel();
        queryNodeLbl = new javax.swing.JLabel();
        setQueryBtn = new javax.swing.JButton();
        inferenceBtn = new javax.swing.JButton();
        inferResultLbl = new javax.swing.JLabel();
        jMenuBarMain = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuItemOpen = new javax.swing.JMenuItem();
        jMenuItemSave = new javax.swing.JMenuItem();
        jMenuEdit = new javax.swing.JMenu();
        jMenuHelp = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        addNodePanel.setBackground(new java.awt.Color(204, 204, 204));

        javax.swing.GroupLayout addNodePanelLayout = new javax.swing.GroupLayout(addNodePanel);
        addNodePanel.setLayout(addNodePanelLayout);
        addNodePanelLayout.setHorizontalGroup(
            addNodePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 139, Short.MAX_VALUE)
        );
        addNodePanelLayout.setVerticalGroup(
            addNodePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout drawPanelLayout = new javax.swing.GroupLayout(drawPanel);
        drawPanel.setLayout(drawPanelLayout);
        drawPanelLayout.setHorizontalGroup(
            drawPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 790, Short.MAX_VALUE)
        );
        drawPanelLayout.setVerticalGroup(
            drawPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        actionsPanel.setBackground(new java.awt.Color(204, 204, 204));
        actionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Node Actions"));

        deleteSelectedBtn.setText("Delete selected");
        actionsPanel.add(deleteSelectedBtn);

        deleteAllBtn.setText("Delete all");
        actionsPanel.add(deleteAllBtn);

        applyLayoutBtn.setText("Apply layout");
        actionsPanel.add(applyLayoutBtn);

        jButtonRefresh.setText("Refresh");
        actionsPanel.add(jButtonRefresh);

        queryPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        queryNodeLbl.setText("Query Node: null");

        setQueryBtn.setText("Set Query");

        inferenceBtn.setText("Infer");

        inferResultLbl.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout queryPanelLayout = new javax.swing.GroupLayout(queryPanel);
        queryPanel.setLayout(queryPanelLayout);
        queryPanelLayout.setHorizontalGroup(
            queryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(queryPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(queryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inferResultLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(queryPanelLayout.createSequentialGroup()
                        .addComponent(setQueryBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(inferenceBtn))
                    .addGroup(queryPanelLayout.createSequentialGroup()
                        .addComponent(queryNodeLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 145, Short.MAX_VALUE)))
                .addContainerGap())
        );
        queryPanelLayout.setVerticalGroup(
            queryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(queryPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(queryNodeLbl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(inferResultLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 257, Short.MAX_VALUE)
                .addGroup(queryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(setQueryBtn)
                    .addComponent(inferenceBtn))
                .addContainerGap())
        );

        javax.swing.GroupLayout panelContainerPanelLayout = new javax.swing.GroupLayout(panelContainerPanel);
        panelContainerPanel.setLayout(panelContainerPanelLayout);
        panelContainerPanelLayout.setHorizontalGroup(
            panelContainerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelContainerPanelLayout.createSequentialGroup()
                .addComponent(queryPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(actionsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        panelContainerPanelLayout.setVerticalGroup(
            panelContainerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelContainerPanelLayout.createSequentialGroup()
                .addComponent(actionsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(queryPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jMenuFile.setText("File");

        jMenuItemOpen.setText("Open file...");
        jMenuFile.add(jMenuItemOpen);

        jMenuItemSave.setText("Save");
        jMenuFile.add(jMenuItemSave);

        jMenuBarMain.add(jMenuFile);

        jMenuEdit.setText("Edit");
        jMenuBarMain.add(jMenuEdit);

        jMenuHelp.setText("Help");
        jMenuHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuHelpActionPerformed(evt);
            }
        });
        jMenuBarMain.add(jMenuHelp);

        setJMenuBar(jMenuBarMain);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(addNodePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(drawPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelContainerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(addNodePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panelContainerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(drawPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // TODO: factor this out properly later
    public JPanel getPanelContainer() {
        return panelContainerPanel;
    }

    public void setGraphPanel(GraphPanel panel) {
        this.graphPanel = panel;
    }

    public JPanel getActionsPanel() {
        return actionsPanel;
    }

    public JButton getLayoutButton() {
        return applyLayoutBtn;
    }

    public JButton getDeleteButton() {
        return deleteSelectedBtn;
    }

    public JButton getDeleteAllButton() {
        return deleteAllBtn;
    }

    public JButton getRefreshButton() {
        return jButtonRefresh;
    }

    public JButton getSetQueryButton() {
        return setQueryBtn;
    }
    
    public JButton getSetInferButton() {
        return inferenceBtn;
    }
    
    public javax.swing.JLabel getResultLabel(){
        return inferResultLbl;
    }
    
    /**
     * Adds GraphPanel View to MainJFrame.
     */
    public void initDrawPanel() {
        // adds the graph panel to drawPanel.
        GroupLayout layout = new GroupLayout(drawPanel);
        drawPanel.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(graphPanel)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(graphPanel)
        );
    }
    
    /**
     * Selects whether JButtons or drag-drop NodeLabels used to add
     * nodes to network.
     */
    public void initNodeControls() {
        if (usingButtons) {
            initNodeButtons();
        } else {
            initNodeLbls();
        }    
        // Also initialise the queryResultPanel
        panelContainerPanel.setLayout(new java.awt.BorderLayout());

    }

    public ArrayList<JButton> getNodeButtons() {
        return BtnArr;
    }

    public JMenuItem getMenuSave() {
        return jMenuItemSave;
    }

    public JMenuItem getMenuOpen() {
        return jMenuItemOpen;
    }

    /**
     * Initialise JButtons for adding nodes.
     * This is called if MainJFrame is instantiated with argument 'true'.
     */
    private void initNodeButtons() {
        // Name and label border
        Border addNodeBorder = BorderFactory.createTitledBorder("Add Node");
        addNodePanel.setBorder(addNodeBorder);

        // Set up layout for the 'add nodes' panel
        addNodePanel.setLayout(new BoxLayout(addNodePanel, BoxLayout.Y_AXIS));

        // Set up a dummy JButton for dynamically adding 
        // buttons in loop.
        JButton dummybtn;

        for (String s : Predef.getVariableTypes()) {
            dummybtn = new JButton(s);
            BtnArr.add(dummybtn);
        }
        // Add buttons.
        for (JButton b : BtnArr) {
            addNodePanel.add(b);
        }
        addNodePanel.setVisible(true);
    }
    /**
     * Initialise drag-drop labels for adding nodes.
     * This is called if MainJFrame is instantiated with argument 'false'.
     */
    private void initNodeLbls() {
        if (graphPanel == null){
            System.err.println("GraphPanel not initialised.");
            return;
        }
        // Name and label border
        Border addNodeBorder = BorderFactory.createTitledBorder("Add Node");
        addNodePanel.setBorder(addNodeBorder);

        // Set up layout for the 'add nodes' panel
        addNodePanel.setLayout(new BoxLayout(addNodePanel, BoxLayout.Y_AXIS));

        // Set up a dummy JLabel for dynamically adding 
        // buttons in for loop.
        ArrayList<NodeLabel> LblArr = new ArrayList<>();
        NodeLabel dummylbl;

        // Construct radio buttons for each predefined type
        // and add to button group.
        for (String s : Predef.getVariableTypes()) {
            dummylbl = new NodeLabel(graphPanel, s);
            LblArr.add(dummylbl);
        }
        // Add buttons.
        for (NodeLabel b : LblArr) {
            addNodePanel.add(b);
        }

        addNodePanel.setVisible(true);
    }

    public void setQueryLbl(String txt){
        queryNodeLbl.setText(txt);
    }
    
    public void setInferResultLbl (String txt){
        inferResultLbl.setText(txt);
    }
    
//    @Override
    public void update() {
        //graphPanel's rendernetwork should be here...
        for (Object cell : graphPanel.getAllVertices()) {
//             ((mxCell) cell).setValue("rofl");
        }
    }

//    @Override
    public void setSubject(Observable sub) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void jMenuHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuHelpActionPerformed
        String message = "<html>"
                + "Basic Controls<br>"
                + "<Mouse scroll> Zoom screen";
        System.err.println("content pane " + getContentPane());
        JOptionPane.showMessageDialog(getContentPane(), message);


    }//GEN-LAST:event_jMenuHelpActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel actionsPanel;
    private javax.swing.JPanel addNodePanel;
    private javax.swing.JButton applyLayoutBtn;
    private javax.swing.JButton deleteAllBtn;
    private javax.swing.JButton deleteSelectedBtn;
    private javax.swing.JPanel drawPanel;
    private javax.swing.JLabel inferResultLbl;
    private javax.swing.JButton inferenceBtn;
    private javax.swing.JButton jButtonRefresh;
    private javax.swing.JMenuBar jMenuBarMain;
    private javax.swing.JMenu jMenuEdit;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenu jMenuHelp;
    private javax.swing.JMenuItem jMenuItemOpen;
    private javax.swing.JMenuItem jMenuItemSave;
    private javax.swing.JPanel panelContainerPanel;
    private javax.swing.JLabel queryNodeLbl;
    private javax.swing.JPanel queryPanel;
    private javax.swing.JButton setQueryBtn;
    // End of variables declaration//GEN-END:variables

}
